package com.upup.controller;

import com.blade.mvc.annotation.*;
import com.blade.mvc.http.Request;
import com.blade.mvc.http.Response;
import com.blade.mvc.multipart.FileItem;
import com.upup.model.Emotion;
import com.upup.service.*;
import com.upup.service.impl.*;
import com.upup.util.*;

import java.io.FileOutputStream;
import java.util.List;
import java.util.Optional;

/**
 * @author : zhiye
 * @description :Index's route
 */

@Path("find")
public class FindController {
    @PostRoute("addemotion")
    @JSON
    public void register(Request request, @Param String place, @Param String n_username, @Param String thetext, Response response){
        EmotionService emotionService = (EmotionService) new EmotionServiceImpl();
        String pic= ATool.getTime();
        String time= ATool.getTime1();
        String username = n_username;
        Optional<FileItem> fileItem = request.fileItem( "avatar");
        fileItem.ifPresent(item ->{
            System.out.println(item);
            byte[] data = item.getData();
            try {
                FileOutputStream fos = new FileOutputStream("E:/Javaidea/chlapp/src/main/resources/static/uploads/"+pic+".jpg");
                fos.write(data);
                fos.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        });
        if (emotionService.saveEmotion(username,place,thetext,time,pic)){
            response.json("{\"message\" : \"添加成功\"}");
        }
    }
    @PostRoute("getemotion")
    @JSON
    public void getemotion(@Param String place,Response response){
        System.out.println(place);
        EmotionService emotionService = new EmotionServiceImpl();
//        System.out.println(emotionService.getEmotions(place));
        List<Emotion> emotions = emotionService.getEmotions(place);
        response.json("{\"sss\" :"+emotions+"}");
    }
}
